<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where" @keydown.enter.prevent="reload">
          <a-row>
            <a-form-item label="标题:">
              <a-input v-model:value.trim="where.messageTitle" placeholder="请输入通知标题" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center" :wrapper-col="{ span: 24 }">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="messageId"
          :datasource="url"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
          class="ant-table-striped"
          :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped-0' : 'table-striped-1')"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space style="margin-right: 30px;">
              <a-radio-group v-model:value="where.readFlag" button-style="solid" @change="reload" default-value="0">
                <a-radio-button value="0">未读</a-radio-button>
                <a-radio-button value="1">已读</a-radio-button>
              </a-radio-group>
            </a-space>

            <a-space v-if="where.readFlag !== '1'">
              <a-button type="primary" @click="setAlready()">
                <template #icon>
                  <CheckOutlined />
                </template>
                <span>全部已读</span>
              </a-button>
            </a-space>
          </template>

          <!-- table列表已读状态 -->
          <template #state="{ record }">
            <a-tag color="green" v-if="record.readFlag === 0">未读</a-tag>
            <a-tag v-else> 已读</a-tag>
          </template>

          <!-- table操作栏按钮 -->
          <template #action="{ record }">
            <a-space>
              <a @click="openEdit(record)">查看</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此消息吗？" @confirm="deleteMessage(record)">
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>
  <mynotice-detail v-model:visible="showDetail" :data="current" @done="reload"></mynotice-detail>
</template>

<script>
import { CheckOutlined } from '@ant-design/icons-vue';
import { NoticeApi } from '@/api/NoticeApi';
import MynoticeDetail from '@/views/notice/mynotice/mynotice-detail';
import { EventBus } from '@/event/eventBus';


export default {
  name: 'Publish',
  components: {
    MynoticeDetail,
    CheckOutlined
  },
  data() {
    return {
      // 表格数据接口
      url: '/sysMessage/page',
      // 表格列配置
      columns: [
        {
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        {
          title: '标题',
          dataIndex: 'messageTitle'
        },
/*        {
          title: '发消息人',
          dataIndex: 'sendUserName'
        },*/
        {
          title: '消息发送时间',
          dataIndex: 'messageSendTime'
        },
        {
          title: '已读状态',
          dataIndex: 'readFLag',
          slots: { customRender: 'state' }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          slots: { customRender: 'action' }
        }
      ],
      // 表格搜索条件
      where: {
        readFlag: "0"
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示详情
      showDetail: false
    };
  },

  watch:{
    '$route'(to, from) {
      // 当路由发生变化时，这里会被调用
      if(to.path === '/notice/mynotice'){
        console.log('路由切换到报表页面', from.path, to.path);
        // 重新加载当前页面
        location.reload();
      }
    },
  },

  methods: {
    /**
     * 重新加载
     *
     * @author laihongliang
     * @date 2021/04/19 00:21
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置
     *
     * @author laihongliang
     * @date 2021/04/19 00:21
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 设置为已读
     *
     * @author fengshuonan
     * @date 2021/6/15 23:08
     */
    async setAlready() {
      let result = await NoticeApi.setAlreadyReadState();
      this.$message.success(result.message);
      EventBus.emit('read-message', '全部已读');
      this.reload();
    },

    /**
     * 打开编辑页面
     *
     * @author fengshuonan
     * @date 2021/6/15 23:14
     */
    openEdit(record) {
      this.current = record;
      this.showDetail = true;
    },

    /**
     * 删除消息
     *
     * @author fengshuonan
     * @date 2021/6/14 22:31
     */
    async deleteMessage(record) {
      let result = await NoticeApi.deleteMessage(record);
      this.$message.success(result.message);
      this.reload();
    }
  }
};
</script>

<style scoped>
/*搜索框与表格之间的间隙*/
.block-interval {
  margin-bottom: 10px;
}

/* 表格斑马样式 **/
/deep/.ant-table-tbody tr:nth-child(2n)
{
  background-color:#fafafa;
}
</style>
